<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="平台名称" prop="deliveryName">
        <el-input v-model="queryParams.deliveryName" placeholder="请输入平台名称" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="平台编号" prop="deliveryCode">
        <el-input v-model="queryParams.deliveryCode" placeholder="请输入平台编号" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!-- 防止仅有一个输入框时光标在那一个输入框里时按回车出现“正在加载系统资源，请耐心等待” -->
      <input hidden />

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['delivery:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['delivery:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['delivery:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="60" />
      <el-table-column label="平台名称" prop="deliveryName" />
      <el-table-column label="平台编号" prop="deliveryCode" />
      <el-table-column label="排序" prop="sort" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['delivery:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['delivery:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />

    <!-- 添加或编辑弹框 -->
    <el-dialog :title="addDialogTitle" :visible.sync="addDialogVisible" width="500px" append-to-body
      @closed="handleAddDialogClosed">
      <el-form ref="addFormRef" :model="addDialogFormData" :rules="addDialogFormRules" size="medium"
        label-width="100px">
        <el-form-item label="平台名称" prop="deliveryName">
          <el-input v-model="addDialogFormData.deliveryName" placeholder="请输入平台名称" clearable> </el-input>
        </el-form-item>
        <el-form-item label="平台编号" prop="deliveryCode">
          <el-input v-model="addDialogFormData.deliveryCode" placeholder="请输入平台编号" clearable> </el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="addDialogFormData.sort" :min="0" placeholder="排序" step-strictly
            controls-position="right"> </el-input-number>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="handleAddDialogCancel">取消</el-button>
        <el-button type="primary" @click="handleAddDialogConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 快递平台管理

import { postDeliveryList, postDeliveryInfo, postDeliveryAdd, postDeliveryEdit, postDeliveryRemove } from '@/api/delivery'

export default {
  name: 'CustomerList',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deliveryName: undefined, // 平台名字
        deliveryCode: undefined, // 平台编号
      },
      // 数据列表
      dataList: [],

      // 生产角色下所有用户列表
      scUserList: [],

      // 添加弹框标题
      addDialogTitle: '',
      // 添加弹框是否显示
      addDialogVisible: false,
      // 添加弹框表单参数
      addDialogFormData: {
        id: undefined, // 数据id，编辑时使用
        deliveryName: undefined, // 快递名称
        deliveryCode: undefined, // 快递编号
        sort: undefined, // 排序
      },
      // 添加弹框表单校验
      addDialogFormRules: {
        deliveryName: [{ required: true, message: '请输入平台名称', trigger: 'blur' }],
        deliveryCode: [{ required: true, message: '请输入平台编号', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    /** 查询列表 */
    getDataList() {
      this.loading = true
      postDeliveryList(this.queryParams).then((response) => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getDataList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$modal
        .confirm('是否确认删除快递平台ID为"' + ids + '"的数据项？')
        .then(function () {
          return postDeliveryRemove({ ids })
        })
        .then(() => {
          this.getDataList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },

    /** 添加弹框关闭了 */
    handleAddDialogClosed() {
      // console.log('添加弹框关闭了')
      this.addDialogFormData = {
        id: undefined, // 数据id，编辑时使用
        deliveryName: undefined, // 快递名称
        deliveryCode: undefined, // 快递编号
        sort: undefined, // 排序
      }
      this.$refs.addFormRef.resetFields()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.addDialogVisible = true
      this.addDialogTitle = '添加快递平台'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let rowData

      if (row.id) {
        rowData = row
      } else {
        rowData = this.dataList.find((item) => item.id === this.ids[0])
      }

      for (const key in this.addDialogFormData) {
        this.addDialogFormData[key] = rowData[key]
      }

      this.addDialogVisible = true
      this.addDialogTitle = '修改快递平台'
    },
    // 添加或编辑弹框取消
    handleAddDialogCancel() {
      this.addDialogVisible = false
    },
    // 添加或编辑弹框确定
    handleAddDialogConfirm() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return

        // console.log(JSON.stringify(this.addDialogFormData))
        // return

        this.loading = true
        if (this.addDialogFormData.id) {
          const result = await postDeliveryEdit(this.addDialogFormData)
          this.$modal.msgSuccess('修改成功')
        } else {
          const result = await postDeliveryAdd(this.addDialogFormData)
          this.$modal.msgSuccess('添加成功')
        }
        this.loading = false
        this.getDataList()
        this.handleAddDialogCancel()
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
